﻿using Domain.User;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.Account;
using ReturnValue;

namespace MES.Server.API.Application.Handler.Account
{
    public class MoreDelHandler : IRequestHandler<MoreDelCommand, ApiResult<int>>
    {
        private readonly IRepository<AccountModel> accountway;
        private readonly ILogger<MoreDelHandler> log;
     

        public MoreDelHandler(IRepository<AccountModel> accountway, ILogger<MoreDelHandler> log)
        {
            this.accountway = accountway;
            this.log = log;
        
        }
        /// <summary>
        /// 实现账号批量删除
        /// </summary>
        /// <param name="request">各个主键</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>影响行数</returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<int>> Handle(MoreDelCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
            List<long>? ids = request.Ids.Split(',',StringSplitOptions.RemoveEmptyEntries).Select(long.Parse).ToList();
            var i = 0;
            foreach (long item in ids)
            {
                var value =await accountway.GetValue(item);
                value.IsDel = true;
                i+= await accountway.UpdateAsync(value);
             
            }
            if(i>0)
            {
                log.LogInformation($"{DateTime.Now}:账号批量删除成功");  //记录日志
                res.Data = i;
                res.Code = StatusCode.Success;           
                res.Message = $"{DateTime.Now}:账号批量删除成功";
            }
            else
            {
                res.Code = StatusCode.Fail;
                res.Message = $"{DateTime.Now}:账号批量删除失败";
            }
            return res;
        }
    }
}
